package com.moyu.user.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.moyu.common.core.domain.R;
import com.moyu.user.domain.permission.Permission;
import com.moyu.user.domain.permission.dto.PermissionRoleDTO;
import com.moyu.user.service.IPermissionService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 赵彦博
 * @Date 2024/11/20 8:44
 */

@RestController
@RequestMapping("permissions")
@Tag(name = "权限接口")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;


    @GetMapping("list")
    public R<List<Permission>> listPermission(@RequestParam(defaultValue = "false") boolean treeMode) {
        return R.ok(permissionService.list(treeMode));
    }

    @GetMapping("roles")
    public List<PermissionRoleDTO> listRole() {
        return permissionService.listRoleByType(Permission.Type.resource);
    }

    @SaCheckRole("权限管理员")
    @PostMapping
    public R<Permission> createPermission(@RequestBody  Permission permission) {
        //todo 分类型参数校验
        return R.ok(permissionService.create(permission));
    }

    @SaCheckRole("权限管理员")
    @PutMapping
    public R<Permission> updatePermission(@RequestBody Permission permission) {
        return R.ok(permissionService.update(permission));
    }

    @SaCheckRole("权限管理员")
    @DeleteMapping("{id}")
    public R deletePermission(@PathVariable Long id) {
        permissionService.delete(id);
        return R.ok();
    }
}
